# coding=utf-8
__author__ = 'golsee'
__create_date__ = '2015-05-06'

"""
    服务器信息
"""
import psutil
from flask_script import Manager

ServerInfoManager = Manager(help=u'获取服务器信息')


@ServerInfoManager.command
def mem():
    u'内存使用'
    phymem = psutil.phymem_usage()
    buffers = getattr(psutil, 'phymem_buffers', lambda: 0)()
    cached = getattr(psutil, 'cached_phymem', lambda: 0)()
    used = phymem.total - (phymem.free + buffers + cached)
    line = " Memory: %5s%% %6s/%s" % (
        phymem.percent,
        str(int(used / 1024 / 1024)) + "M",
        str(int(phymem.total / 1024 / 1024)) + "M"
    )
    return line


@ServerInfoManager.command
def cpu(interval=1):
    u'cpu使用'
    return (" CPU: " + str(psutil.cpu_percent(interval,percpu=True)) + "%")

